package com.syq.face.utils;

import android.media.FaceDetector;

import androidx.loader.app.LoaderManager;

import com.syq.face.model.Location;
import com.syq.face.service.FaceService;
import com.syq.face.service.FileService;
import com.syq.face.service.LocationService;
import com.syq.face.service.UserService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * @author ShangMeichun
 */
public class RetrofitUtil {
    /**
     * 服务器url
     */
    private static final String BASE_URL = "http://x.x.x.x:x";


    private static RetrofitUtil instance;
    private Retrofit retrofit;
    private UserService userService;
    private FileService fileService;
    private FaceService faceService;
    private LocationService locationService;



    public static String getBaseUrl() {
        return BASE_URL;
    }

    public static RetrofitUtil getInstance() {
        if (instance == null) {
            synchronized (RetrofitUtil.class) {
                if (instance == null) {
                    instance = new RetrofitUtil();
                    instance.init();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化retrofit
     */
    private void init() {
        if (retrofit == null) {
            //初始化一个OkHttpClient
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(30000, TimeUnit.MILLISECONDS)
                    .readTimeout(30000, TimeUnit.MILLISECONDS)
                    .writeTimeout(30000, TimeUnit.MILLISECONDS);
            //添加拦截器放在这里
            //builder.addInterceptor();
            OkHttpClient okHttpClient = builder.build();
            //配置时间参数
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            //使用该OkHttpClient创建一个Retrofit对象
            retrofit = new Retrofit.Builder()
                    //添加标准数据格式转换器，防止String字符串被直接转化为json格式
                    .addConverterFactory(ScalarsConverterFactory.create())
                    //添加Gson数据格式转换器支持
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    //指定网络请求client
                    .client(okHttpClient)
                    .baseUrl(BASE_URL)
                    .build();
            userService = retrofit.create(UserService.class);
            fileService = retrofit.create(FileService.class);
            faceService=retrofit.create(FaceService.class);
            locationService=retrofit.create(LocationService.class);

        }
    }

    /**
     * 将一个json字符串转化为RequestBody
     * 该json字符串的转化工具需要前后端统一
     *
     * @param json
     * @return
     */
    public static RequestBody jsonParseToBody(String json) {
        return RequestBody.create(MediaTypeUtil.JSON, json);
    }

    /**
     * 将一个字符串(text/plain)转化为RequestBody
     *
     * @param text 字符串
     * @return
     */
    public static RequestBody textParseToBody(String text) {
        return RequestBody.create(MediaTypeUtil.TEXT, text);
    }

    /**
     * 将文件转化为RequestBody类型
     * 然后进行上传或者进一步转化成MultipartBody.Part
     *
     * @param file 要转化的文件
     * @return
     */
    public static RequestBody fileParseToBody(File file) {
        return RequestBody.create(MediaTypeUtil.FILE, file);
    }

    /**
     * 将文件转化为MultipartBody.Part进行上传
     *
     * @param name 服务器上面的key
     * @param file 要上传的文件
     * @return
     */
    public static MultipartBody.Part fileParseToMultipartBody(String name, File file) {
        RequestBody requestBody = fileParseToBody(file);
        return MultipartBody.Part.createFormData(name, file.getName(), requestBody);
    }




    public Retrofit getRetrofit() {
        return retrofit;
    }

    public UserService getUserService() {
        return userService;
    }

    public FileService getFileService() {
        return fileService;
    }

    public FaceService getFaceService(){
        return faceService;
    }

    public LocationService getLocationService() {
        return locationService;
    }
}
